package auctionsimulation.controller;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import auctionsimulation.agent.BuyAgent;
import auctionsimulation.agent.SellAgent;
import auctionsimulation.market.MarketServer;
import auctionsimulation.statistics.Statistics;

public class SimulationController {
	
	private static ExecutorService executor;

	public static void main(String[] args) {

		// specify which logging level messages shall be shown
		Logger.getGlobal().setLevel(Level.OFF);

		executor = Executors.newCachedThreadPool();

		Runnable server = new MarketServer();
		executor.execute(server);

		startSellAgents("sellagent3", 20);
		startBuyAgents("buyagent3", 15);
		startSellAgents("sellagent1", 20);
		startBuyAgents("buyagent2", 15);
		startSellAgents("sellagent2", 20);
		startBuyAgents("buyagent1", 15);
		
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
		}

		Statistics.print();
		System.exit(0);
		/*
		 * executor.execute(new BuyAgent("buyagent1"));
		 * 
		 * executor.execute(new BuyAgent("buyagent1"));
		 * 
		 * executor.execute(new BuyAgent("buyagent1"));
		 */

	}
	
	private static void startSellAgents(String configFileName, int amount) {
		for(int i = 0; i < amount; i++) {
			executor.execute(new SellAgent(configFileName));
			try {
				Thread.sleep(new Random().nextInt(10));
			} catch (InterruptedException e) {
			}
		}
	}
	
	private static void startBuyAgents(String configFileName, int amount) {
		for(int i = 0; i < amount; i++) {
			executor.execute(new BuyAgent(configFileName));
			try {
				Thread.sleep(new Random().nextInt(10));
			} catch (InterruptedException e) {
			}
		}
	}

}
